package com.stu.day17;

import java.util.ArrayList;
import java.util.List;

/**
 * ArrayList线程不安全
 * @author zy
 * @date 2025/2/17 15:44
 */
public class ArrayListSafeDemo implements Runnable{
    //集合中的add方法 =>  size = s
    // elementData[s] = e;
    // size = s + 1;// 不是原子性操作

    private ArrayList<Integer> list = new ArrayList<>();//线程不安全

    //private List<Integer> list = new Vector<>();//线程安全的  add 使用synchronized

    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            list.add(i);
        }
        System.out.println(Thread.currentThread().getName()+":集合大小:"+list.size()+",集合内容:"+list);
        list.size();
    }
}
class ArrayListSafeDemoTest{
    public static void main(String[] args) {
        Runnable r = new ArrayListSafeDemo();
        Thread t1 = new Thread(r);
        Thread t2 = new Thread(r);
        t1.start();
        t2.start();
    }
}
